<?php

/**
 * 自动生成 MerFuncController
 * 
 * Apps/Backend/Controller/MerFuncController.class.php
 * Zix @ 2015-06-29
 */

namespace Backend\Controller;

use Common\Service\SysFuncService;
use Common\Service\MerchantService;

class MerFuncController extends BackendController {

  public function index() {
    $action = I('get.action', 'read');

    $data = $this->_init_data();

    $data['module_name'] = '商户功能';
    $data['module_url'] = U($this->base_path . 'index') ;

    $SysFunc = SysFuncService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => $this->menu_id ,
      'module_url' => U($this->base_path . 'index'),
      'read_url' => U($this->base_path . 'read'),
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'mer_operation_url' => U( $this->module .'/MerFuncOperation/index' ) . '/',
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $SysFunc->get_default_row()
    );

    $data['param'] = array(
      'main_menu' => $SysFunc->get_by_cond(array(
        'sys_type' => 3 ,
      )),
      'status' => $SysFunc->status ,
    );

    $this->display_with_layout( $data, $this->controller . ':index');
  }

  function read() {
    $post = I('post.');

    $config = array(
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'sys_type' => 3 ,
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $SysFunc = SysFuncService::instance();
    $data['rows'] = $SysFunc->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $SysFunc = SysFuncService::instance();
    $post['sys_type'] = 3 ;
    if ( empty( $post['pid'] ) ) {
      $post['level'] = 1 ;
      unset($post['pid']);
    } else {
      $post['level'] = $SysFunc->get_level($post['pid']);
    }
    
    $ret = $SysFunc->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $SysFunc = SysFuncService::instance();
    $post['sys_type'] = 3 ;
    if ( empty( $post['pid'] ) ) {
      $post['level'] = 1 ;
      unset($post['pid']);
    } else {
      $post['level'] = $SysFunc->get_level($post['pid']);
    }
    $post['update_time'] = date('Y-m-d H:i:s');
    $ret = $SysFunc->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $SysFunc = SysFuncService::instance();
    $ret = $SysFunc->delete($ids);
    $this->ajaxReturn($ret);
  }

}
